<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :custom-class="'panna-dialog_big'"
      :append-to-body="true"
      :title="dataList.CreateMainRequest"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <div style="float: left">
        <el-button type="primary" @click="choiceDevice">{{
          dataList.SelectDevice
        }}</el-button>
      </div>
      <br />
      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        height="130px"
      >
        >
        <el-table-column :label="dataList.DeviceNumber">
          <template slot-scope="scope">
            {{ scope.row.num }}
          </template>
        </el-table-column>
        <el-table-column
          :label="
            $i18n.locale === 'en' ? dataList.DeviceEName : dataList.DeviceName
          "
        >
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceSpecifications">
          <template slot-scope="scope">
            {{ scope.row.specifications }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceSupplierName">
          <template slot-scope="scope">
            {{ scope.row.supplierName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceStatus">
          <template slot-scope="scope">
            {{ scope.row.statusName }}
          </template>
        </el-table-column>
        <el-table-column
          :label="
            $i18n.locale === 'en'
              ? dataList.eDeviceStorageAddress
              : dataList.DeviceStorageAddress
          "
        >
          <template slot-scope="scope">
            {{ scope.row.storageAddress }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DevicePersonResponsibleId">
          <template slot-scope="scope">
            {{ scope.row.personResponsibleName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Devicedepart">
          <template slot-scope="scope">
            {{ scope.row.departName }}
          </template>
        </el-table-column>

        <el-table-column :label="dataList.Operation">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index)"
                >{{ btn.Delete }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <br />

      <el-form
        ref="formData"
        class="base-pipeline-layout system-user-form"
        :label-width="$i18n.locale === 'en' ? '180px' : '100px'"
        :model="formData"
        :rules="formDataRules"
        status-icon
      >
        <el-form-item
          style="width: 100%"
          :label="dataList.MainReason"
          prop="reason"
          :error="errorMsg.reason"
        >
          <el-input
            v-model="formData.reason"
            type="textarea"
            style="float: left; width: 43%"
          ></el-input>
        </el-form-item>
        <el-form-item
          style="width: 100%"
          :label="dataList.LastCalibDate"
          prop="calibrationDate"
          :error="errorMsg.calibrationDate"
        >
          <el-date-picker
            v-model="formData.calibrationDate"
            style="float: left; width: 43%"
            type="date"
            :placeholder="dataList.SelectDateTime"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item
          style="width: 100%"
          :label="dataList.Deviceremark"
          prop="remark"
          :error="errorMsg.remark"
        >
          <el-input
            v-model="formData.remark"
            type="textarea"
            style="float: left; width: 43%"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit(10)">{{
          btn.Save
        }}</el-button>
        <el-button type="primary" style="width: 140px" @click="onSubmit(20)">{{
          btn.SaveAndSubmit
        }}</el-button>
        <el-button @click="cancel">{{ btn.Cancel }}</el-button>
      </div>
    </el-dialog>
    <DeviceDialogList
      v-if="choiceDeviceVisible"
      :dialog-form-visible.sync="choiceDeviceVisible"
      :choice-device-num.sync="choiceDeviceNum"
      :checked-rows.sync="checkedRows"
      :display-text="displayText"
      @checkedRowsFun="closeChoiceDevice"
    />
  </div>
</template>

<script>
import { create, update } from "../../api/DeviceRepair.js";
import DeviceDialogList from "../../components/DeviceDialogList.vue";

export default {
  name: "DeviceRepairAddDialog",
  components: { DeviceDialogList },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      displayText: [
      this.$t("ModuleDevice.Device.Fault"),
         this.$t("ModuleDevice.Device.Downgrading"),
        this.$t("ModuleDevice.Device.Deactivate"),
      ],
      choiceDeviceNum: 1,
      checkedRows: [],
      loading: false,
      innerDialogFormVisible: false,
      choiceDeviceVisible: false,
      formLabelWidth: "100px",
      list: [],
      initData: {
        id: 0,
        reason: "",
        calibrationDate: null,
        remark: "",
      },
      formData: {
        id: 0,
        reason: "",
        calibrationDate: null,
        remark: "",
        statusId: 0,
        creatorId: "",
        creatorName: "",
        deviceNum: "",
        deviceId: "",
      },
      formDataRules: {
        reason: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
      },
      initErrorMsg: {
        reason: "",
        calibrationDate: "",
        remark: "",
      },
      errorMsg: {
        reason: "",
        calibrationDate: "",
        remark: "",
      },
    };
  },
  computed: {
    btn() {
      return this.$t("ModuleDevice.buttons");
    },
    dataList() {
      return this.$t("ModuleDevice.Device");
    },
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  created() {
    this.innerDialogFormVisible = this.dialogFormVisible;
  },
  methods: {
    handleDelete(index) {
      this.list.splice(index, 1);
    },
    // 关闭选择设备弹框
    closeChoiceDevice(data) {
      data.map((item) => {
        if (item.deviceCalibrationTask != null) {
          this.formData.calibrationDate =
            item.deviceCalibrationTask.calibrationDate;
        }
      });
      this.list = data;
    },
    // 选择设备
    choiceDevice() {
      this.checkedRows = this.list;
      this.choiceDeviceVisible = true;
    },
    cancel() {
      this.$emit("close");
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.cancel();
    },
    onSubmit(statusId) {
      if (this.list === null || this.list.length === 0) {
        this.$message.warning(this.$i18n.t("ModuleDevice.Message.EqiupEmpty"));
        return false;
      }

      this.formData.statusId = statusId;
      this.formData.creatorId = this.$store.getters.currentUserId;
      this.formData.creatorName = this.$store.getters.name;
      this.formData.deviceNum = this.list[0].num;
      this.formData.deviceId = this.list[0].id;

      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("reason", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("calibrationDate", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("remark", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
